#!/usr/bin/env node

console.log("start cli");

const inquirer = require("inquirer");
const chalk = require("chalk");
const ejs = require("ejs");
const fs = require("fs");
const path = require("path");
const log = console.log;
const tempDir = path.join(__dirname, "templates"); //模板目录
const targetDir = process.cwd();

const question = [
  {
    type: "input",
    name: "name",
    message: "项目名?",
  },
  {
    type: "input",
    name: "auther",
    message: "你的名字?",
  },
  {
    type: "checkbox",
    name: "language",
    message: "请选择语言",
    choices: [
      new inquirer.Separator("--------------------\n"),
      { name: "javascript" },
      { name: "typescript" },
    ],
    validate: function (answer) {
      if (answer.length < 1) {
        log(chalk.red("你必须选择一种语言"));
      }
      return true;
    },
  },
];

const getTempletes = () => {};

inquirer.prompt(question).then((answers) => {
  console.log(answers);
  fs.readdir(tempDir, (err, files) => {
    if (err) throw err;
    files.forEach((file) => {
      ejs.renderFile(path.join(tempDir, file), answers, (err, result) => {
        if (err) throw err;

        const dir = fs.mkdirSync(`${targetDir}/${answers.name}`, {
          recursive: true,
        });
        log(dir);
        fs.writeFileSync(path.join(dir, file), result);
      });
    });
  });
});
